(function(angular) {
	'use strict';
	angular.module('fs.waf').factory('attributeService', attributeService);
	attributeService.$inject = ['loggerFactory', 'ajaxServiceFactory', '$q'];
	function attributeService(loggerFactory, ajaxServiceFactory, $q) {
		var log = loggerFactory.getLogger("attributeService");
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			getYesNoOptions : {
				url : 'do/cda/dataList/yesno',
				method : 'get'
			},
			getDynaValueBeans : {
				url : 'do/cda/dataList/dynaValueBeans',
				method : 'get'
			}
		});
		
		function getDynaValueBeans() {
			var defer = $q.defer();
			ajaxService.getDynaValueBeans().then(function(response) {
				defer.resolve(response ? response.data : {});
			});
			return defer.promise;
		};
		
		function dynaparmLookup(){
			return {
				"id" : "cda.dynaparm",
				"params" : {
					"parmCat" : "ATTRIBUTE",
					"serviceCode" : "CDA"
				},
				"boundTo" : {
					"parmId" : "parmId"
				},
				"okEvent" : true,
				"cancelEvent" : false
			};
		}
		
		function initAttributeOption(ctrl, $scope){
			ctrl.parmIdOptions = dynaparmLookup();
			ctrl.parmValueUitypeOptions = getParmValueUitypeOptions();
			
			getDynaValueBeans().then(function(response){
				ctrl.allDynaValueBeans = response;
				ctrl.valueBeanIdOptions = getValueBeanIdOptions(ctrl.allDynaValueBeans);
			});
			
			ctrl.initChangeParmValueUitype = function(dynaparm, $scope){
				if(!ctrl.attribute.dynaparm){
					return;
				}
				$scope.updateParmValueUitype = ctrl.attribute.dynaparm.parmValueUitype;
				
				if(!dynaparm || !ctrl.attribute.dynaparm){
					return null;
				}
				var serviceCode = (dynaparm || ctrl.attribute.dynaparm).serviceCode;
				var parmValueUitype = (dynaparm || ctrl.attribute.dynaparm).parmValueUitype;
				if(!serviceCode || !parmValueUitype){
					return null;
				}
				ctrl.valueBeanIdOptions = [];
				angular.forEach(ctrl.allDynaValueBeans, function(data, index, array){
					var beanType = '';
					if(parmValueUitype == "D"){
						beanType = "JAVA";
					}else if(parmValueUitype == "S"){
						beanType = "POPUP";
					}
					if(data.beanType == beanType && (data.serviceCode == '*' || data.serviceCode == serviceCode)){
						var opt = new Object();
						opt.value = data.valueBeanId;
						opt.label = data.valueBeanName;
						ctrl.valueBeanIdOptions.push(opt);
					}
				});
				if((dynaparm || ctrl.attribute.dynaparm).parmValueUitype == 'D' || (dynaparm || ctrl.attribute.dynaparm).parmValueUitype == 'S'){
					(ctrl.attribute.dynaparm || dynaparm).valueBeanId = (ctrl.attribute.dynaparm || dynaparm).valueBeanId ? (ctrl.attribute.dynaparm || dynaparm).valueBeanId : ctrl.valueBeanIdOptions.data ? ctrl.valueBeanIdOptions.data[0].value : "";
				}
			};
			
			ctrl.changeParmValueUitype = function(){
				ctrl.initChangeParmValueUitype(ctrl, $scope);
			};
			
			ctrl.isShowDropDown = function(){
				if(ctrl.attribute.dynaparm && ctrl.attribute.dynaparm.parmValueUitype == 'L'){
					return true;
				}else{
					return false;
				}
			};
		};
		
		function getValueBeanIdOptions(list) {
			var item = [];
			angular.forEach(list, function(data, index, array){
				var opt = new Object();
				opt.value = parseInt(data.valueBeanId);
				opt.label = data.valueBeanName;
				item.push(opt);
			});
			return item;
		}
		
		function getParmValueUitypeOptions(){
			var data = [
			        {
						"value" : "I",
						"label" : "label.dynaparm.parmvaluetype.input"
					}, {
						"value" : "L",
						"label" : "label.dynaparm.parmvaluetype.dropdown"
					}, {
						"value" : "D",
						"label" : "label.dynaparm.parmvaluetype.dynadropdown"
					}, {
						"value" : "S",
						"label" : "label.dynaparm.parmvaluetype.lookup"
					}];
			return data;
		};
		return {
			initAttributeOption : initAttributeOption
		};
	}
})(angular);